import { IHttpResponse } from '@ibiz-template/core';
import {
  MDControlService,
  UIMapField,
  ControlVO,
} from '@ibiz-template/runtime';
import { IDEGrid } from '@ibiz/model-core';

/**
 * 表格部件服务
 * @author lxm
 * @date 2023-05-15 09:53:35
 * @export
 * @class GridService
 * @extends {MDControlService<IDEGrid>}
 */
export class GridService extends MDControlService<IDEGrid> {
  /**
   * 初始化属性映射
   *
   * @author lxm
   * @date 2022-08-31 18:08:37
   */
  initUIDataMap(): void {
    super.initUIDataMap();
    // *初始化表格数据项的属性映射
    this.model.degridDataItems?.forEach(column => {
      const uiKey = column.id!.toLowerCase();
      // todo
      const deField = column.appDEFieldId;
      let mapField: UIMapField;
      // 后台实体属性
      if (deField) {
        const deFieldKey = deField.toLowerCase();
        mapField = new UIMapField(uiKey, deFieldKey, {
          isOriginField: true,
        });
      } else {
        // 前台属性
        mapField = new UIMapField(uiKey, uiKey);
      }
      this.dataUIMap.set(uiKey, mapField);
    });

    //* 如果属性列和表格数据列名称不一致，补充不一致的属性列的名称映射
    // this.model.fieldColumns.forEach(fieldColumn => {
    //   if (fieldColumn.source.dataItemName !== fieldColumn.codeName) {
    //     const value = this.dataUIMap.get(fieldColumn.source.dataItemName);
    //     if (value) {
    //       this.dataUIMap.set(fieldColumn.codeName, value);
    //     }
    //   }
    // });
  }

  /**
   * 编辑列更新
   *
   * @author lxm
   * @date 2022-09-15 21:09:34
   * @param {string} methodName
   * @param {IParams} context
   * @param {IParams} [params={}]
   * @returns {*}  {Promise<IHttpResponse<ControlVO>>}
   */
  async updateGridEditItem(
    methodName: string,
    context: IParams,
    params: IParams = {},
  ): Promise<IHttpResponse<ControlVO>> {
    let res = await this.entityService.exec(methodName, context, params);
    res = this.handleResponse(res);
    return res as IHttpResponse<ControlVO>;
  }
}
